package com.example.project3.service.im;

import com.example.project3.entity.GroupChat;
import com.example.project3.entity.WorkGroup;
import com.example.project3.mapper.GroupChatMapper;
import com.example.project3.mapper.WorkGroupMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @ClassName IMController .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/6/6 19:21
 * @Version 1.0
 */
@Service
@Slf4j
@ServerEndpoint("/link/{uid}")
public class IMService {
    private static final ConcurrentMap<Integer, IMService> SESSION_POOL = new ConcurrentHashMap<>();
    private Integer uid;
    private Session session;
    private WorkGroupMapper workGroupMapper;
    private ObjectMapper jacksonObjectMapper;
    private GroupChatMapper groupChatMapper;


    /**
     * 链接websocket时调用
     *
     * @param session
     * @param uid
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "uid") Integer uid) {
        try {
            this.session = session;
            this.uid = uid;
            SESSION_POOL.put(uid, this);
            log.info("uid:{} online,online population:{}", uid, SESSION_POOL.size());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误", e);
        }
    }

    @OnClose
    public void onClose() {
        SESSION_POOL.remove(this.uid);
        log.info("uid:{} offline,online population:{}", this.uid, SESSION_POOL.size());
    }

    /**
     * 组发
     *
     * @param groupChat
     */
    public void sendGroup(GroupChat groupChat) throws IOException {
        Map<String, Object> query = new HashMap<>();
        query.put("project_id", groupChat.getProjectId());
        List<WorkGroup> list = workGroupMapper.selectByMap(query);
        for (WorkGroup wg : list) {
            if (wg.getUid() == groupChat.getFromUid()) {
                continue;
            }
            if (SESSION_POOL.containsKey(wg.getUid())) {
                SESSION_POOL.get(wg.getUid()).session.getBasicRemote().sendText(jacksonObjectMapper.writeValueAsString(groupChat));
            }
        }
        groupChatMapper.insert(groupChat);
    }

    @Autowired
    public void setWorkGroupMapper(WorkGroupMapper workGroupMapper) {
        this.workGroupMapper = workGroupMapper;
    }

    @Autowired
    public void setJacksonObjectMapper(ObjectMapper jacksonObjectMapper) {
        this.jacksonObjectMapper = jacksonObjectMapper;
    }

    @Autowired
    public void setGroupChatMapper(GroupChatMapper groupChatMapper) {
        this.groupChatMapper = groupChatMapper;
    }
}
